//
//  KKAccountController.m
//  DoMi
//
//  Created by 康凯 on 2016/11/11.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKAccountController.h"
#import "KKMyHttp.h"
#import "KKBillCell.h"
#import "KKTiXianVC.h"
#import "KKRechargeVC.h"

@interface KKAccountController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *currentPrice;
@property (weak, nonatomic) IBOutlet UIView *rechargeView;
@property (weak, nonatomic) IBOutlet UIView *popMoneyView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) int page;
@property (strong, nonatomic)NSMutableArray *datas;

@end

@implementation KKAccountController
#pragma mark 懒加载
-(NSArray *)datas{
    if(!_datas){
        _datas=[NSMutableArray array];
    }
    return _datas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的帐户";
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(recharge)];
    [self.rechargeView addGestureRecognizer:tap];
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(popMoney)];
    [self.popMoneyView addGestureRecognizer:tap1];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.rowHeight=41;
    [self.tableView registerNib:[UINib nibWithNibName:@"KKBillCell" bundle:nil] forCellReuseIdentifier:@"KKBillCell"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getAccountDetail)];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self loadMoreData];
    }];
    [self getCurrentPrice];
}

#pragma mark 充值点击
-(void)recharge{
    KKRechargeVC *recharge=[[KKRechargeVC alloc] init];
    [self.navigationController pushViewController:recharge animated:YES];
}
#pragma mark 提现点击
-(void)popMoney{
    KKTiXianVC *tiXian=[[KKTiXianVC alloc] init];
    [self.navigationController pushViewController:tiXian animated:YES];
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKBillCell *billCell=(KKBillCell *)cell;
    [billCell initAccountData:self.datas[indexPath.row]];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"KKBillCell"];
}

#pragma mark 接口--当前余额
-(void)getCurrentPrice{
    [KKMyHttp getCurrentPriceSuccess:^(id data) {
        self.currentPrice.text=[NSString stringWithFormat:@"¥%@",data];
    } failure:^(NSError *error) {
        
    }];
}
#pragma mark 接口--帐户明细
-(void)getAccountDetail{
    [KKMyHttp getAccountDetail:@"1" success:^(id data) {
        self.page=1;
        [self.datas removeAllObjects];
        [self.datas addObjectsFromArray:data];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
#pragma mark 接口--帐户明细
-(void)loadMoreData{
    [KKMyHttp getAccountDetail:[NSString stringWithFormat:@"%d",self.page] success:^(id data) {
        [self.datas addObjectsFromArray:data];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

@end
